package com.monco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.monco.entity.Goods;
import com.monco.mapper.GoodsMapper;
import com.monco.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author com/monco
 * @since 2020-04-19
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<Goods> getGoodsList(Goods goods) {
        return goodsMapper.selectList(getWrapper(goods));
    }

    @Override
    public IPage<Goods> getGoodsPage(IPage<Goods> page, Goods goods) {
        return goodsMapper.selectPage(page, getWrapper(goods));
    }

    private QueryWrapper<Goods> getWrapper(Goods goods) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(goods.getGoodsName())) {
            queryWrapper.like("goods_name", goods.getGoodsName());
        }
        if (StringUtils.isNotBlank(goods.getGoodsType())) {
            queryWrapper.eq("goods_type", goods.getGoodsType());
        }
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }
}
